﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using LostHobbits.Scene;
using Microsoft.Xna.Framework.Content;

namespace LostHobbits.Render
{
    public class RenderManager : DrawableGameComponent
    {
        public struct RendationInfo
        {
            public string alias;
            public Color tint;
            public float rotation;
            public Vector2 location;
        }

        private Dictionary<string, Sprite> sprites = new Dictionary<string,Sprite>();
        private ISceneService scene;
        private ContentManager content;
        private SpriteBatch spriteBatch;

        public RenderManager(Game game)
            :base(game)
        {           
            scene = (ISceneService)game.Services.GetService(typeof(ISceneService));
            spriteBatch = spriteBatch = new SpriteBatch(game.GraphicsDevice);
            content = game.Content;
        }

        public void LoadSprite(string alias)
        {            
            Texture2D spriteTexture = content.Load<Texture2D>(alias);
            Sprite newSprite = new Sprite(spriteTexture, spriteTexture.Bounds);
            sprites.Add(alias, newSprite);          
        }

        public void LoadSprite(string alias, Rectangle[] animation)
        {
            
        }

        public override void  Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            foreach (RendationInfo toRender in scene.RendationAssembly())
            {
                if (!sprites.ContainsKey(toRender.alias))
                    LoadSprite(toRender.alias);
                sprites[toRender.alias].Draw(spriteBatch, toRender.tint, toRender.rotation, toRender.location);
            }
            spriteBatch.End();
 	        base.Draw(gameTime);

        }
    }
}
